package creationalPatterns.prototype;

import java.util.HashMap;
import java.util.Map;

class AbstractShape implements Cloneable {
    private String type;

    public AbstractShape(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public AbstractShape clone() {
        try {
            AbstractShape clone = (AbstractShape) super.clone();
            clone.setType(type);
            return clone;
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
    }
}

class Circle extends AbstractShape {
    public Circle() {
        super("Circle");
    }
}

class Rectangle extends AbstractShape {
    public Rectangle() {
        super("Rectangle");
    }
}

class ShapeCache {
    private static final ShapeCache shapeCache = new ShapeCache();
    private final Map<String, AbstractShape> shapes = new HashMap<>();

    private ShapeCache() {
        shapes.put("Circle", new Circle());
        shapes.put("Rectangle", new Rectangle());
        System.out.println(shapes);
    }

    public static ShapeCache getInstance() {
        return shapeCache;
    }

    public AbstractShape getShape(String shapeType) {
        AbstractShape shape = shapes.get(shapeType);
        return shape.clone();
    }
}

public class PrototypePattern {
    public static void main(String[] args) {
        ShapeCache shapeCache = ShapeCache.getInstance();
        AbstractShape shape1 = shapeCache.getShape("Circle");
        AbstractShape shape2 = shapeCache.getShape("Rectangle");
        System.out.println(shape1);
        System.out.println(shape2);
    }
}